<template>
  <BasicModal v-bind="$attrs" @register="registerModal" showFooter title="新增增删改查按钮菜单" width="35%" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup name="ButtonModal">
import { ref } from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { crudFormSchema } from './index-option';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { saveCrudMenuApi } from '/@/api/sys/menu';
import { useMessage } from '/@/hooks/web/useMessage';

const { createMessage } = useMessage();

const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 100,
  schemas: crudFormSchema,
  showActionButtonGroup: false,
  baseColProps: { lg: 24, md: 24 },
});

const menuId = ref();

const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  resetFields();
  menuId.value = data.record.menuId;
  setFieldsValue({
    ...data.record,
  });
  setModalProps({ confirmLoading: false });
});

const emit = defineEmits(['success']);

async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    const param = { ...values, menuId: menuId.value };
    const res = await saveCrudMenuApi(param);
    createMessage.success(res.msg);
    closeModal();
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
